package com.tourismhengyang.actions;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.tourismhengyang.beans.PlaneTicket;
import com.tourismhengyang.beans.TrainTicket;
import com.tourismhengyang.biz.IPlaneTicketBiz;
import com.tourismhengyang.biz.ITrainTicketBiz;
import com.tourismhengyang.impl.PlaneTicketBizImpl;
import com.tourismhengyang.impl.TrainTicketBizImpl;
import com.tourismhengyang.utils.JsonObject;

public class TravelTicketAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private ITrainTicketBiz trainTicketBiz = new TrainTicketBizImpl();
	private IPlaneTicketBiz planeTicketBiz = new PlaneTicketBizImpl();
	private TrainTicket tt;
	private PlaneTicket pt;
	private HttpSession session;
	private JsonObject json;
	
	public String searchTrainTickets() {
		List<TrainTicket> result = trainTicketBiz.getTrainTickets(tt);
		json = new JsonObject();
		if(result != null) {
			json.setRows(result);
			json.setTotal(result.size());
		} else {
			json.setRows(null);
		}
		return SUCCESS;
	}
	
	public String searchPlaneTickets() {
		List<PlaneTicket> result = planeTicketBiz.getPlaneTickets(pt);
		json = new JsonObject();
		if(result != null) {
			json.setRows(result);
			json.setTotal(result.size());
		} else {
			json.setRows(null);
		}
		return SUCCESS;
	}
	
	public String getTicketInfo() {
		session = ServletActionContext.getRequest().getSession();
		TrainTicket t = trainTicketBiz.getOneTicket(tt);
		PlaneTicket p = planeTicketBiz.getOneTicket(pt);
		json = new JsonObject();
		if(t!=null){
			session.setAttribute("T_TicketInfo", t);
			json.setResult(1);
		}else if(p!=null){
			session.setAttribute("P_TicketInfo", p);
			json.setResult(1);
		}else{
			json.setResult(0);
		}
		return SUCCESS;
	}

	public TrainTicket getTt() {
		return tt;
	}

	public void setTt(TrainTicket tt) {
		this.tt = tt;
	}
	
	public PlaneTicket getPt() {
		return pt;
	}

	public void setPt(PlaneTicket pt) {
		this.pt = pt;
	}


	public JsonObject getJson() {
		return json;
	}

	public void setJson(JsonObject json) {
		this.json = json;
	}
}
